package com.bawei.module_mall

import android.view.View
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mall.databinding.ActivityOrderSheetBinding
import com.bawei.module_mall.manager.OrderManager
import com.bumptech.glide.Glide
import android.app.Activity
import android.content.Intent
import com.bawei.lib_mvi.Router

@Route(path = "/module_shop/OrderSheetActivity")
class OrderSheetActivity : BaseActivity() {

    companion object {
        private const val ADDRESS_REQUEST_CODE = 1001
    }

    @Autowired
    @JvmField
    var goodsName: String? = null
    @Autowired
    @JvmField
    var goodsDesc: String? = null
    @Autowired
    @JvmField
    var goodsPrice: Double = 0.0
    @Autowired
    @JvmField
    var goodsCount: Int = 1
    @Autowired
    @JvmField
    var goodsImg: String? = null

    private val binding by lazy {
        ActivityOrderSheetBinding.inflate(layoutInflater)
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        setupUI()
        setupClickListeners()
    }

    private fun setupUI() {
        // 设置商品信息到UI
        binding.tvGoodsName.text = goodsName
        binding.tvGoodsDesc.text = goodsDesc
        binding.tvGoodsPrice.text = "¥$goodsPrice"
        binding.tvCount.text = goodsCount.toString()
        binding.tvGoodsTotal.text = "¥$goodsPrice"
        binding.tvTotal.text = "¥$goodsPrice"
        binding.tvBottomPrice.text = "¥$goodsPrice"
        
        // 加载图片
        goodsImg?.let {
            Glide.with(this).load(it).into(binding.ivGoods)
        }
    }

    private fun setupClickListeners() {
        // 地址栏点击跳转到地址管理
        binding.layoutAddress.setOnClickListener {
            ARouter.getInstance()
                .build(Router.Ui.ADDRESS_ACTIVITY)
                .navigation(this, ADDRESS_REQUEST_CODE)
        }
        
        // 返回按钮
        binding.ivBack.setOnClickListener {
            finish()
        }
        
        // 提交订单按钮
        binding.btnSubmit.setOnClickListener {
            submitOrder()
        }
    }

    private fun submitOrder() {
        // 检查是否选择了收货地址
        if (binding.tvName.text.isNullOrEmpty() || binding.tvAddress.text.isNullOrEmpty()) {
            Toast.makeText(this, "请先选择收货地址", Toast.LENGTH_SHORT).show()
            return
        }

        // 创建订单
        val order = OrderManager.createOrder(
            sellerName = "官方商城", // 这里可以根据实际情况设置卖家名称
            productTitle = goodsName ?: "",
            productPrice = "¥$goodsPrice",
            quantity = goodsCount,
            tag1 = goodsDesc ?: "",
            tag2 = "7天无理由退货",
            productImageUrl = goodsImg
        )

        Toast.makeText(this, "订单提交成功！", Toast.LENGTH_SHORT).show()
        
        // 跳转到订单列表页面
        ARouter.getInstance()
            .build("/module_mall/OrderListActivity")
            .navigation()
        
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ADDRESS_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            val name = data.getStringExtra("addressName")
            val phone = data.getStringExtra("addressPhone")
            val detail = data.getStringExtra("addressDetail")
            binding.tvName.text = name
            binding.tvPhone.text = phone
            binding.tvAddress.text = detail
        }
    }
}